class UnionFind:
    def __init__(self, n):
        self.fa = list(range(n + 1))
        self.size = [1] * (n + 1)

    def find(self, x):
        if self.fa[x] != x:
            self.fa[x] = self.find(self.fa[x])
        return self.fa[x]

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x == root_y:
            return False
        self.fa[root_y] = root_x
        self.size[root_x] += self.size[root_y]
        return True


n, m, k = map(int, input().split())
uf = UnionFind(n)
edges = []
for _ in range(m):
    x, y, l = map(int, input().split())
    edges.append((x, y, l))
edges.sort(key=lambda x: -x[2])  # 降序排列方便弹出
costs = 0
t = 0
while t < n - k and edges:
    u, v, w = edges.pop()
    if uf.union(u, v):
        costs += w
        t += 1

print(costs)
